package com.runen.infinitemovement.widget.popupwind;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.tool.CommonUtil;

public class HozomPoppupWindow extends PopupWindow {
    private TextView tv_contribution, tv_hozom_1, tv_hozom_10, tv_hozom_100;
    private LinearLayout ll_hozom_1, ll_hozom_10, ll_hozom_100;
    private Context context;
    private View layout;
    private EditText edt_input_num;
    private ImageView iv_dismiss;

    public OnHozonClickListener getOnHozonClickListener() {
        return onHozonClickListener;
    }

    public void setOnHozonClickListener(OnHozonClickListener onHozonClickListener) {
        this.onHozonClickListener = onHozonClickListener;
    }

    private OnHozonClickListener onHozonClickListener;

    public static HozomPoppupWindow getInstance(Context context) {
        return new HozomPoppupWindow(context);
    }

    private HozomPoppupWindow(Context context) {
        super(context);
        this.context = context;
        initView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //this.setHeight((int) (CommonUtil.getScreenHW((Activity) context)[1] / 3));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 这样设置才能点击屏幕外dismiss窗口
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(layout);
        initEvent();
    }

    private void initView() {
        layout = View.inflate(context, R.layout.popup_hozom_info, null);
        tv_contribution = layout.findViewById(R.id.tv_contribution);

        ll_hozom_1 = layout.findViewById(R.id.ll_hozom_1);
        ll_hozom_10 = layout.findViewById(R.id.ll_hozom_10);
        ll_hozom_100 = layout.findViewById(R.id.ll_hozom_100);

        tv_hozom_1 = layout.findViewById(R.id.tv_hozom_1);
        tv_hozom_10 = layout.findViewById(R.id.tv_hozom_10);
        tv_hozom_100 = layout.findViewById(R.id.tv_hozom_100);

        iv_dismiss = layout.findViewById(R.id.iv_dismiss);

        edt_input_num = layout.findViewById(R.id.edt_input_num);
    }

    private String num = "1";

    private void initEvent() {

        tv_contribution.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = edt_input_num.getText().toString().trim();
                if (!TextUtils.isEmpty(string)) {
                    num = string;
                }
                if (onHozonClickListener != null) {
                    onHozonClickListener.onHozomClick(Integer.parseInt(num));
                }

            }
        });

        //关闭Dialog
        ll_hozom_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ll_hozom_1.setBackground(CommonUtil.getDrawable(R.drawable.bg_ffff8c1f_360dp));
                ll_hozom_10.setBackground(CommonUtil.getDrawable(R.drawable.bg_ffffeedd_17dp));
                ll_hozom_100.setBackground(CommonUtil.getDrawable(R.drawable.bg_ffffeedd_17dp));
                tv_hozom_1.setTextColor(CommonUtil.getColor(R.color.white));
                tv_hozom_10.setTextColor(CommonUtil.getColor(R.color.color_FFFF8C1F));
                tv_hozom_100.setTextColor(CommonUtil.getColor(R.color.color_FFFF8C1F));
                num = "1";
            }
        });
        ll_hozom_10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ll_hozom_1.setBackground(CommonUtil.getDrawable(R.drawable.bg_ffffeedd_17dp));
                ll_hozom_10.setBackground(CommonUtil.getDrawable(R.drawable.bg_ffff8c1f_360dp));
                ll_hozom_100.setBackground(CommonUtil.getDrawable(R.drawable.bg_ffffeedd_17dp));
                tv_hozom_1.setTextColor(CommonUtil.getColor(R.color.color_FFFF8C1F));
                tv_hozom_10.setTextColor(CommonUtil.getColor(R.color.white));
                tv_hozom_100.setTextColor(CommonUtil.getColor(R.color.color_FFFF8C1F));
                num = "10";
            }
        });
        ll_hozom_100.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ll_hozom_1.setBackground(CommonUtil.getDrawable(R.drawable.bg_ffffeedd_17dp));
                ll_hozom_10.setBackground(CommonUtil.getDrawable(R.drawable.bg_ffffeedd_17dp));
                ll_hozom_100.setBackground(CommonUtil.getDrawable(R.drawable.bg_ffff8c1f_360dp));
                tv_hozom_1.setTextColor(CommonUtil.getColor(R.color.color_FFFF8C1F));
                tv_hozom_10.setTextColor(CommonUtil.getColor(R.color.color_FFFF8C1F));
                tv_hozom_100.setTextColor(CommonUtil.getColor(R.color.white));
                num = "100";

            }
        });
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               dismiss();
            }
        });

    }

    public void show() {
//        this.setAnimationStyle(R.style.Animation);
        //防止部分手机底部状态栏与popupWindow重叠
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setAnimationStyle(R.style.AnimationBottomFade);
        //popupWindow底部显示
        this.showAtLocation(((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();

        lp.alpha = 0.5f;
        if (lp.alpha == 1) {
            ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        ((Activity) context).getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity) context).getWindow().setAttributes(lp);
    }

    public interface OnHozonClickListener {
        void onHozomClick(int number);
    }
}
